package pluginUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ApollonianGasker implements Iterable<AGCircle>{
	
	private List<SoddyTriplet> enAttente=new ArrayList<SoddyTriplet>();
	private int levels; 
	private ArrayList<AGCircle> toDraw=new ArrayList<AGCircle>(); 
	private double minRad=0.00005; 
	private boolean existsExternal=false; 
	private AGCircle external; 
	
	public boolean existsExternal(){return this.existsExternal;}
	
	public AGCircle getExternal(){return external;}
	
	public ApollonianGasker(SoddyTriplet st,int levels,double minRad){
		this(st,levels); 
		this.minRad=minRad;
	}
	
	public ApollonianGasker(SoddyTriplet st,int levels){
		enAttente.add(st);
		this.levels=levels;
		this.minRad=minRad;
		if(st.getA().isInverted()){ existsExternal=true; this.external=st.getA();}
		if(st.getB().isInverted()){ existsExternal=true; this.external=st.getB();}
		if(st.getC().isInverted()){ existsExternal=true; this.external=st.getC();}
	}
	
	public void makeGasket(){
		for(int i=levels;i>0;i--){
			ArrayList<SoddyTriplet> provisoire=new ArrayList<SoddyTriplet>(); 
			for(SoddyTriplet s:enAttente){
				AGCircle pro=s.getMini();
				if(pro.getRadius()>minRad){
				toDraw.add(s.getMini());
			
				AGCircle a=s.getA(); 
				AGCircle b=s.getB(); 
				AGCircle c=s.getC();
			
				
				if(!toDraw.contains(a)&& !a.isInverted()){toDraw.add(a);} 
				if(!toDraw.contains(b) && !b.isInverted()){toDraw.add(b);} 
				if(!toDraw.contains(c)&&! c.isInverted()){toDraw.add(c);} 
				

			    provisoire.add(new SoddyTriplet(a,b,pro)); 
				provisoire.add(new SoddyTriplet(a,c,pro));
			    provisoire.add(new SoddyTriplet(c,b,pro));
				}
			
			}// for
		enAttente=provisoire; 	
		}
	}
	
public Iterator<AGCircle> iterator(){return this.toDraw.iterator(); }	
	
public ArrayList<AGCircle> getResult(){return this.toDraw; }	
}
